//============================================================================
// Name        : Card.cpp
// Author      : awhuck
// Version     :
//============================================================================

#include "card/card.h"
#include "image_manager.h"
#include "graphics/graphics.h"
#include "object.h"
#include "draw/SDL_draw.h"
#include "SDL/SDL_ttf.h"
#include "card/card_info.h"

///Constructor
Card::Card(CardInfo* info) : 
	Object(ImageManager::GetManager()->GetImage( IMAGE_CARD_BACKGROUND ), OBJECT_ID_CARD, false, false)
{
	// make a copy of the official image
	Surface* surface = ImageManager::GetManager()->CreateImage(
			ImageManager::GetManager()->GetImage( IMAGE_CARD_BACKGROUND )->GetWidth(),
			ImageManager::GetManager()->GetImage( IMAGE_CARD_BACKGROUND )->GetHeight());
	ImageManager::GetManager()->GetImage( IMAGE_CARD_BACKGROUND )->Apply(surface, 0, 0);
	SetSurface(surface, true);
	
	this->info = info;
	
	DrawCard();
	
	TooltipInfo* tooltipInfo = new TooltipInfo();
	Graphics::GetGraphics()->CreateStandardTooltip(tooltip, &(tooltipInfo->surface), &(tooltipInfo->background));
	SetTooltip(tooltipInfo);
}

Card::~Card()
{
	if( info )
		delete info;
}

CardInfo* Card::GetInfo()
{
	return info;
}

void Card::DrawCard()
{
	int x = CARD_CONTENT_X; int y = CARD_CONTENT_Y;
	int w; int h;
	Graphics::GetGraphics()->DrawFont(GetSurface(), info->GetCardName(), CARD_NAME_FONT, CARD_NAME_FONT_SIZE, CARD_NAME_COLOR, x, y);
	Graphics::GetGraphics()->GetFontSize(CARD_NAME_FONT, info->GetCardName(), CARD_NAME_FONT_SIZE, &w, &h);
	y += h + 1;
	tooltip = info->GetCardName() + "\n";
	
	DrawPattern( CARD_PATTERN_INDENT_X, y + CARD_PATTERN_INDENT_Y );
	
	DrawEffects( &x, &y );
	
	tooltip += "\n" + info->GetCardDescription();
}

void Card::DrawPattern(int xPos, int yPos)
{
	Color color;
	
	for( int i = 0, x = xPos; i < CARD_PATTERN_DIMENSION; i++, x += CARD_PATTERN_SIZE - 1 )
		for( int j = 0, y = yPos; j < CARD_PATTERN_DIMENSION; j++, y += CARD_PATTERN_SIZE - 1 )
		{
			Draw_Rect(GetSurface()->GetSurface(), x, y, CARD_PATTERN_SIZE, CARD_PATTERN_SIZE, 0);
			
			switch (info->GetCardCost()[i][j])
			{
			case 'R':
				color.r = 255; color.g = color.b = 0;
				Draw_FillRect(GetSurface()->GetSurface(), x + 1, y + 1, CARD_PATTERN_SIZE - 2, CARD_PATTERN_SIZE - 2, color.ToUint32());
				break;
			case 'B':
				color.b = 255; color.g = color.r = 0;
				Draw_FillRect(GetSurface()->GetSurface(), x + 1, y + 1, CARD_PATTERN_SIZE - 2, CARD_PATTERN_SIZE - 2, color.ToUint32());
				break;
			case 'Y':
				color.b = 0; color.g = color.r = 255;
				Draw_FillRect(GetSurface()->GetSurface(), x + 1, y + 1, CARD_PATTERN_SIZE - 2, CARD_PATTERN_SIZE - 2, color.ToUint32());
				break;
			case 'G':
				color.g = 255; color.r = color.b = 0;
				Draw_FillRect(GetSurface()->GetSurface(), x + 1, y + 1, CARD_PATTERN_SIZE - 2, CARD_PATTERN_SIZE - 2, color.ToUint32());
				break;
			case 'W':
				color.r = color.g = color.b = 255;
				Draw_FillRect(GetSurface()->GetSurface(), x + 1, y + 1, CARD_PATTERN_SIZE - 2, CARD_PATTERN_SIZE - 2, color.ToUint32());
				break;
			case 'L':
				color.r = color.g = color.b = 0;
				Draw_FillRect(GetSurface()->GetSurface(), x + 1, y + 1, CARD_PATTERN_SIZE - 2, CARD_PATTERN_SIZE - 2, color.ToUint32());
				break;
			}
		}
}

void Card::DrawEffects(int* x, int* y)
{
	tooltip += "\nEffects\n";
	
	int w = 0, h = 0;
	for( int i = 0; i < (int)info->GetEffects().size(); i++ )
	{
		tooltip += info->GetEffects()[i]->GetLongText() + "\n";
		Graphics::GetGraphics()->DrawFont(GetSurface(), info->GetEffects()[i]->GetShortText(), 
				CARD_EFFECTS_FONT, CARD_EFFECTS_FONT_SIZE, CARD_EFFECTS_COLOR, *x + CARD_EFFECTS_INDENT, *y);
		Graphics::GetGraphics()->GetFontSize(CARD_EFFECTS_FONT, info->GetEffects()[i]->GetShortText(), CARD_EFFECTS_FONT_SIZE, &w, &h);
		*y += h + 1;
	}
}
